using System.Text;
using Hangfire;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Radzen;
using Tmp.Models;
using Tmp.Services;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Encoding.GetEncoding("Shift-JIS");
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options => { options.Cookie.IsEssential = true; });
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRadzenComponents();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<ClipboardService>();
builder.Services.AddSingleton<UploadClock>();
builder.Services.AddSingleton<DownloadLimit>();
builder.Services.Configure<Configuration>(dom =>
{
    dom.Site = builder.Configuration["Domain"];
    dom.UploadPath = builder.Configuration["Upload"];
    dom.MaxFileSize = 30 * 1024 * 1024;
});
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddSwaggerGen(opts =>
{
    opts.SwaggerDoc("v1", new OpenApiInfo()
    {
        Title = "Dispatcher",
        Version = "v1"
    });
});
builder.Services.AddHangfire(configuration => configuration
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseSqlServerStorage(builder.Configuration.GetConnectionString("TmpConnection")))
    ;
builder.Services.AddHangfireServer();
builder.Services.AddDbContext<DataContext>(opts =>
{
    opts.UseSqlServer(builder.Configuration.GetConnectionString("TmpConnection"));
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();

}
app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseHangfireDashboard("/dashboard");
app.UseRouting();

app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapBlazorHub();
    endpoints.MapDefaultControllerRoute();
    endpoints.MapRazorPages();
    endpoints.MapFallbackToPage("/_Host");
    endpoints.MapHangfireDashboard();
});
app.UseSwagger();
app.UseSwaggerUI(opts => { opts.SwaggerEndpoint("/swagger/v1/swagger.json", "tmp"); });
RecurringJob.AddOrUpdate<UploadClock>("fetchKey",clock=> clock.Reset(),Cron.Daily);
app.Run();


// {
//     "iisSettings": {
//         "windowsAuthentication": false,
//         "anonymousAuthentication": true,
//         "iisExpress": {
//             "applicationUrl": "http://localhost:37071",
//             "sslPort": 44309
//         }
//     },
//     "profiles": {
//         "Tmp": {
//             "commandName": "Project",
//             "dotnetRunMessages": true,
//             "launchBrowser": true,
//             "applicationUrl": "https://localhost:7042;http://localhost:5041",
//             "environmentVariables": {
//                 "ASPNETCORE_ENVIRONMENT": "Development"
//             }
//         },
//         "IIS Express": {
//             "commandName": "IISExpress",
//             "launchBrowser": true,
//             "environmentVariables": {
//                 "ASPNETCORE_ENVIRONMENT": "Development"
//             }
//         }
//     }
// }


// {
//     "iisSettings": {
//         "windowsAuthentication": false,
//         "anonymousAuthentication": true,
//         "iisExpress": {
//             "applicationUrl": "http://localhost:37071",
//             "sslPort": 44309
//         }
//     },
//     "profiles": {
//         "Tmp": {
//             "commandName": "Project",
//             "dotnetRunMessages": true,
//             "launchBrowser": true,
//             "applicationUrl": "https://localhost:7042;http://localhost:5041",
//             "environmentVariables": {
//                 "ASPNETCORE_ENVIRONMENT": "Production"
//             }
//         },
//         "IIS Express": {
//             "commandName": "IISExpress",
//             "launchBrowser": true,
//             "environmentVariables": {
//                 "ASPNETCORE_ENVIRONMENT": "Production"
//             }
//         }
//     }
// }
